import axios from 'axios'
import { ElMessage } from 'element-plus'

const instance = axios.create({
    withCredentials: true,  // 携带token
    timeout: 1000,
    baseURL: ''
})
instance.defaults.headers.post = {
    'Content-Type': 'application/x-www-form-urlencoded'
}
instance.defaults.headers.common = {
    'Auth-Type': 'company-web',
    'X-Requested-With': 'XMLHttpRequest',
    token: 'sdfjlsdfjlsdjflsjflsfjlskd'
}
instance.interceptors.request.use(
    config => config,
    error => error
)
instance.interceptors.response.use(
    res => {
        if (res.status === 200)
            ElMessage.success("请求成功")
        return res
    },
    err => {
        const { response } = err
        errorHandle(response.status, response.data)
        return response
    }
)
const errorHandle = (status: Number, other: any) => {
    switch (status) {
        case 400:
            ElMessage.error('信息校验失败')
            break
        case 401:
            ElMessage.error('认证失败')
            break
        case 403:
            ElMessage.error('token校验失败')
            break
        case 404:
            ElMessage.error('请求的资源不存在')
            break
        default:
            ElMessage.error(other)
            break
    }
}
export default instance
